package com.hongyan.dao;

import com.hongyan.bean.Document;
import com.hongyan.util.JDBCUtils;

import java.util.List;

public class DocumentDao {
    public static Object[][] getDocumentList() {

        String sql = "select * from document order by d_id asc;";
        List<Document> query = null;
        query = JDBCUtils.query(sql, Document.class);
        Object[][] documents = new Object[query.size()][];
        for (int i = 0; i < query.size(); i++) {
            documents[i] = query.get(i).toArray();
        }
        return documents;

    }

    public static Object[][] findDocumentByName(String name) {

        String sql = "select * from document where d_name like '%" + name + "%' order by d_id asc;";
        List<Document> query = null;
        query = JDBCUtils.query(sql, Document.class);
        Object[][] documents = new Object[query.size()][];
        for (int i = 0; i < query.size(); i++) {
            documents[i] = query.get(i).toArray();
        }
        return documents;


    }

    public static Document findDocumentById(String id) {

        String sql = "select * from document where d_id ='" + id + "' ";
        Document document = JDBCUtils.queryByconditionOne(sql, Document.class);
        return document;
    }

    public static Object[][] getDocumentListBySql(String sql) {

        List<Document> query = null;
        query = JDBCUtils.query(sql, Document.class);
        Object[][] documents = new Object[query.size()][];
        for (int i = 0; i < query.size(); i++) {
            documents[i] = query.get(i).toArray();
        }
        return documents;


    }

    public static int insertDocumentBySql(Document document) {
        String sql = "insert into document(d_id,d_name,upload_time,document_upload) values(?,?,?,?)";
        return JDBCUtils.update(sql, document.getD_id(), document.getD_name(), document.getUpload_time(), document.getDocument_upload());
    }

    public static int deleteDocumentBySql(String d_id) {
        String sql = "delete from document where d_id = ?";
        return JDBCUtils.update(sql, d_id);
    }


}
